Update and Add Data

Check If Record Already Exists Before Saving Data

Description
This customization shows how to verify if a record already exists when inserting a new record.
Variables
RecordControl
Select a record control
Table Name
Select a table to insert record
Primary Key
Select the primary key in the table
Primary Key Control
Select the primary key control in the table
Applies to
RecordControl class
Code
 
''' 
''' This sub is called when a record is getting saved. Call MyBase.SaveData()
''' only if duplicate record is not present in the database.
''' 
Public Overrides Sub SaveData()
    Dim whereStr As String = ${${Table Name}ClassName}.${Primary Key}.UniqueName & " = '" & Me.${Primary Key Control}.Text & "'"

    ' Check if record already exists before saving.
    Dim rec As ${${Table Name}RecordClassName} = ${${Table Name}ClassName}.GetRecord(whereStr)
    If (Not IsNothing(rec)) Then
   
      ' If record exists, do not save the record
    Else
       
       ' Save the record if it does not exists
        MyBase.SaveData()
    End If
End Sub
     

Terms of Service Privacy Statement